<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>Promise testDemo</title>
	<style type="text/css">
	* {
		padding: 0;
		margin: 0;
	}
	 
</style>
</head>
<body>
	<h1>aaaa</h1>
	<div id="box">
	    <h2>第九章 闭包与作用域</h2>
	    <button>收藏课程</button>
	  </div>
</body>
<script src="index.js"></script>
<script type="text/javascript"> 
function ajax(url) {
  console.log(url)
  
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.send();
    xhr.onload = function() {
      if (this.status == 200) {
        resolve(JSON.parse(this.response));
      } else {
        reject(this);
      }
    };
  });
}

console.time('aaa')


// 并行的多个异步ajax操作
const api = "http://localhost/ajax/ajax3.php?type=query";
const promises = ["向军", "后盾人"].map((name,index) => {
  return ajax(`${api}&name=${name}&t=${index+1}`);
});

const hd = Promise.all(promises)
.then(results => {
	console.timeEnd('aaa')
	console.log(results);
})
.catch(msg => {
	console.log(msg);
});

</script>
</html>